package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/7/11 16:31
 * @Version 1.0
 */
@Service
public class ItemServiceImpl implements ItemService {
    //  注入product-client
    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;


    @Override
    public Map getItem(Long skuId) {
        //  创建map 集合
        HashMap<String, Object> map = new HashMap<>();

        //  判断
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConst.SKU_BLOOM_FILTER);
        if (!bloomFilter.contains(skuId)){
            return new HashMap();
        }
        //  skuInfo
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //  获取skuInfo + skuImage
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            map.put("skuInfo",skuInfo);
            return skuInfo;
        },threadPoolExecutor);
        //  categoryView
        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //  获取分类数据
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            map.put("categoryView",categoryView);
        },threadPoolExecutor);
        //  price
        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            //  获取最新价格
            BigDecimal price = productFeignClient.getSkuPrice(skuId);
            map.put("price",price);
        },threadPoolExecutor);
        //  spuPoster
        CompletableFuture<Void> spuPosterListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //  获取海报信息
            List<SpuPoster> spuPosterList = productFeignClient.getSpuPosterBySpuId(skuInfo.getSpuId());
            map.put("spuPosterList",spuPosterList);
        },threadPoolExecutor);
        //  spuSaleAttrList
        CompletableFuture<Void> spuSaleAttrListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //  获取销售属性数据 spuSaleAttrList
            List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            map.put("spuSaleAttrList",spuSaleAttrList);
        },threadPoolExecutor);
        CompletableFuture<Void> valuesSkuJsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //  获取切换Json 字符串
            Map valueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            String valuesSkuJson = JSON.toJSONString(valueIdsMap);
            map.put("valuesSkuJson",valuesSkuJson);
        },threadPoolExecutor);

        CompletableFuture<Void> skuAttrListCompletableFuture = CompletableFuture.runAsync(() -> {
            //  如何获取规格参数.
            List<BaseAttrInfo> attrList = productFeignClient.getAttrList(skuId);
            //  获取平台属性集合
            List<HashMap<String, Object>> skuAttrList = attrList.stream().map(baseAttrInfo -> {
                //  创建map
                HashMap<String, Object> map1 = new HashMap<>();
                map1.put("attrName", baseAttrInfo.getAttrName());
                //  一个skuId 对应的平台属性值只有一个！
                map1.put("attrValue", baseAttrInfo.getAttrValueList().get(0).getValueName());
                return map1;
            }).collect(Collectors.toList());
            //  存储数据
            map.put("skuAttrList",skuAttrList);
        },threadPoolExecutor);

        //  开启异步线程去执行记录商品被访问的次数.
        CompletableFuture<Void> incrCompletableFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId);
        },threadPoolExecutor);
        //  多任务组合
        CompletableFuture.allOf(
                skuInfoCompletableFuture,
                categoryViewCompletableFuture,
                priceCompletableFuture,
                spuPosterListCompletableFuture,
                spuSaleAttrListCompletableFuture,
                valuesSkuJsonCompletableFuture,
                skuAttrListCompletableFuture,
                incrCompletableFuture
                ).join();
        //  返回map
        return map;
    }
}
